package com.bytz.modules.cms.order.schedule;

import cn.hutool.core.bean.BeanUtil;
import com.bytz.common.system.vo.LoginUser;
import com.bytz.common.util.SecurityUtils;
import com.bytz.modules.cms.c4c.model.SalesQuoteIds;
import com.bytz.modules.cms.c4c.service.IC4cFeignService;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.service.IAdminOrderService;
import com.bytz.modules.cms.order.service.IOrderEmailService;
import com.bytz.modules.system.service.ISysUserService;
import com.bytz.modules.system.vo.SysUserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@DisallowConcurrentExecution
public class GetOrderSystemStatusJob implements Job {

    @Autowired
    private IAdminOrderService orderService;

    @Autowired
    private IC4cFeignService c4cService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IOrderEmailService orderEmailService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        SysUserVo syscall = sysUserService.getUserByName("syscall");
        SecurityUtils.setLoginUser(BeanUtil.copyProperties(syscall, LoginUser.class));

        List<Order> orders = orderService.getSystemStatusSyncList();
        orders.forEach(order -> {
            try {
                SalesQuoteIds quoteIds = c4cService.getQuoteReference(order.getSapQuotationId(), order.getQuotationId());
                order.setC4cSalesOrderId(quoteIds.getC4cSalesOrderId());
                // 第一次获取到sap报价 证明在spa报价已经创建则发邮件给订单管理下的订单录入
                if (StringUtils.isBlank(order.getSapQuotationId()) && StringUtils.isNotBlank(quoteIds.getSapQuotationId())) {
                    order.setSapQuotationId(quoteIds.getSapQuotationId());
                    order.setSysStatus(OrderConstants.SysStatus.SAP_QUOTATION);
                    orderEmailService.sendSalesQuoteEmail(order);
                }
                String status = order.getStatus();
                boolean statusFlag = OrderConstants.OrderStatus.YI_YU_FU.equals(status) || OrderConstants.OrderStatus.DAI_TI_JIAO_WEI_KUAN.equals(status) || OrderConstants.OrderStatus.YI_WAN_CHENG.equals(status);
                // 如果sap订单已创建 且订单状态为已预付 将订单状态改为排产中
                if (StringUtils.isBlank(order.getSapSalesOrderId()) && StringUtils.isNotBlank(quoteIds.getSapSalesOrderId()) && statusFlag) {
                    // sap订单已创建之后给对应的销售邮件
                    order.setSapSalesOrderId(quoteIds.getSapSalesOrderId());
                    order.setSysStatus(OrderConstants.SysStatus.SAP_ORDER);
                    orderEmailService.sendSapOrderEmail(order);
                    if (OrderConstants.OrderStatus.YI_YU_FU.equals(status)) {
                        order.setStatus(OrderConstants.OrderStatus.PAI_CHAN_ZHONG);
                    }
                }
            } catch (Exception e) {
                log.error("更新 sap报价单id、sap订单id、c4c订单id失败, orderId=" + order.getId(), e);
            }
        });
        orderService.updateBatchById(orders);

        SecurityUtils.setLoginUser(null);
    }
}
